﻿using System;
using System.ComponentModel;
using System.Drawing;
using Weapp.ControlLibrary.CustomBasicControlers;

namespace Weapp.ControlLibrary
{
    public partial class TextField : BaseField
    {
        #region Constructors
        public TextField()
        {
            InitializeComponent();
            EnabledTextFieldBackColor = Color.White;
            customMaskedTextBoxFieldValue.BackColor = enabledTextFieldBackColor;
        }
        #endregion

        #region Private Members
        private Color enabledTextFieldBackColor;
        #endregion

        #region Properties
        [Description("The field value (displayed on top of the line)."), Category("Appearance")]
        public string FieldValue
        {
            get { return customMaskedTextBoxFieldValue.Text; }
            set { customMaskedTextBoxFieldValue.Text = value; } 
        }
        [Description("Sets the input format allowed to this control."), Category("Behavior")]
        public Mask Masked
        {
            get { return customMaskedTextBoxFieldValue.Masked; }
            set { customMaskedTextBoxFieldValue.Masked = value; }
        }
        [DefaultValue(typeof(Color), "White")]
        [Description("The background color of the control's textbox when it's enabled."), Category("Appearance")]
        public Color EnabledTextFieldBackColor
        {
            get { return enabledTextFieldBackColor; }
            set { enabledTextFieldBackColor = value; }
        }
        #endregion

        #region Events
        //
        // Summary:
        //     Raises the System.Windows.Forms.TextBox.TextAlignChanged event.
        //
        // Parameters:
        //   e:
        //     An System.EventArgs that contains the event data.
        protected override void BaseField_TextAlignChanged(EventArgs e)
        {
            customMaskedTextBoxFieldValue.TextAlign = TextAlign;
            base.BaseField_TextAlignChanged(e);
        }
        protected override void BaseField_SizeChanged(object sender, EventArgs e)
        {
            if (customMaskedTextBoxFieldValue!=null)
            {
                Height = customMaskedTextBoxFieldValue.Font.Height + LabelFieldName.Height + 1; 
            }
            base.BaseField_SizeChanged(sender, e);
        }
        protected override void BaseField_BackColorChanged(object sender, EventArgs e)
        {
            customMaskedTextBoxFieldValue.BackColor = BackColor;
            base.BaseField_BackColorChanged(sender, e);
        }
        protected override void BaseField_ForeColorChanged(object sender, EventArgs e)
        {
            customMaskedTextBoxFieldValue.ForeColor = ForeColor;
            base.BaseField_ForeColorChanged(sender, e);
        }
        private void TextField_FontChanged(object sender, EventArgs e)
        {
            customMaskedTextBoxFieldValue.Font = new Font(Font.FontFamily, Font.Size, Font.Style);
            Size = new Size(Width, customMaskedTextBoxFieldValue.Height + LabelFieldName.Height + 1);
        }
        protected override void BaseField_EnabledChanged(object sender, EventArgs e)
        {
            customMaskedTextBoxFieldValue.Enabled = Enabled;
            if (Enabled)
	        {
		        customMaskedTextBoxFieldValue.BackColor = EnabledTextFieldBackColor;
	        }
            else
            {
                customMaskedTextBoxFieldValue.BackColor = BackColor;
            }
            base.BaseField_EnabledChanged(sender, e);
        }
        #endregion
    }
}
